#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
//就和有个指针指向尾部一样static tail 这里是用的一个节点但是并不接入链表 data统计个数
typedef struct Node {
    int data;
    struct Node *next;
} Node;

/*list data记录个数
 *1. list->1->2->3
 *2. 1->2->3->4->list
*/

Node * initList() 
{
    Node *list = (Node *)malloc(sizeof(Node));
    list->data = 0;
    list->next = list; 
    return list;  
}

void headInsert(Node *list, int data) 
{   //此时list指针相当于是尾指针
    Node *node = (Node *)malloc(sizeof(Node));
    node->data = data;
    node->next = list->next->next;
    list->next->next = node;
    list->data++;
}

void tailInsert(Node *list, int data) 
{
    Node *tmp = list;
    Node *node = (Node *)malloc(sizeof(Node));
    node->data = data;
    list->next->next = node;
    list->next = node;
    list->data++;
}
 /*do {
        printf("%d ", node->data);
    } while ((node=node->next));
    */ //不太好理解

int del(Node *list, int data) 
{
    Node *pre = list;
    Node *cur = list->next;
    while (cur != list) {
        if (cur->data == data) {
            pre->next = cur->next;
            free(cur);
            return true;
        }
        pre = cur;
        cur = cur->next;
    }
    return false;
}

void printList(Node *list) 
{
    Node *node = list->next;
    while(node != list) {
        printf("%d->", node->data);
        node = node->next;
    }
    printf("NULL\n");    
}
int main(void)
{
    Node *list = initList();
    headInsert(list, 1);
    headInsert(list, 2);
    headInsert(list, 3);
    headInsert(list, 4);
    headInsert(list, 5);
    
    printList(list);
  
    printf("\n个数：%d", list->data);
    return 0;
}